// packageF/others/customerCenter/component/userDetail/myFavorite/myFavorite.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keywork: {
      type: null
    },
    memberId: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    strUrl: "plugin.wechat-chat-sidebar.frontend.controller.customer.customer.favorite",
    getConfig: {
      client: "work",
      type: 17
    }, //企业微信公共参数
    collectionList: [],
    json: {
      page: 1
    },
    loading: false,
    finished: false
  },
  lifetimes: {
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en,
      });
      this.getData();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getData() {
      if (this.data.finished && this.data.loading) {
        return;
      }
      this.data.loading = true;
      let json = {};
      if (this.data.keywork == 'customer') {
        this.data.strUrl = 'plugin.customer-center.frontend.controllers.customer.favorite';
        this.data.json.member_id = this.data.memberId;
        json = {
          ...this.data.json
        };
      }
      let urlStr = app.getNetAddresss(this.data.strUrl);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1 && res.data.data) {
            if (this.data.json.page >= res.data.last_page || res.data.data.length < res.data.per_page) {
              this.setData({
                finished: true
              })
            }
            this.setData({
              collectionList: this.data.json.page > 1 ? this.data.collectionList.concat(res.data.data) : res.data.data,
              'json.page': ++this.data.json.page
            })
          } else {
            this.setData({
              finished: true
            })
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    },
    loadMore() {
      this.getData();
    }
  }
})